﻿function showDiscussList() {
	if (refreshTab('showDiscussList')) return;
	var pageSize = 30;

	/* region tree */
	var cm = [new Ext.list.Column({
						header : 'Title',
						dataIndex : 'title'
					}), new Ext.list.Column({
						header : 'Length',
						dataIndex : 'len',
						width : 60
					}), new Ext.list.Column({
				header : 'Problem',
				dataIndex : 'pid',
				width : 60,
				tpl : new Ext.XTemplate('<a href="javascript:void(0);" onclick="showDiscuss({pid});"'
						+ ' title="点击查看讨论" class="likelink">' + '{pid}</a>')
			}), new Ext.list.Column({
				header : 'ProblemTitle',
				dataIndex : 'problemTitle',
				width : 150,
				tpl : new Ext.XTemplate('<a href="javascript:void(0);" onclick="showProblem({pid});"'
						+ ' title="点击查看题目" class="likelink">' + '{problemTitle}</a>')
			}), new Ext.list.Column({
				header : 'Author',
				dataIndex : 'username',
				width : 100,
				tpl : new Ext.XTemplate('<a href="javascript:void(0);" onclick="showUser(\'{username}\');"'
						+ ' title="{[Ext.isEmpty(values.nickname)?values.username:values.nickname]}" class="likelink">'
						+ '{[Ext.isEmpty(values.nickname)?values.username:values.nickname]}</a>')
			}), new Ext.list.Column({
						header : 'Time',
						dataIndex : 'time',
						width : 150
					})];

	var root = new Ext.tree.AsyncTreeNode({
				id : 'root',
				expanded : true
			});

	var store = new Ext.tree.TreeLoaderStore({
				rootNode : root,
				loader : new Ext.tree.TreePagingLoader({
							dataUrl : url.getDiscussList
						})
			});
	store.load({
				params : {
					start : 0,
					limit : pageSize
				}
			});

	var pagebar = new Ext.PagingToolbar({
				pageSize : pageSize,
				store : store,
				displayInfo : true,
				emptyMsg : '系统没有帖子。',
				displayMsg : '显示第 {0} - {1} 篇帖子，共 {2} 帖。',
				plugins : new Ext.ux.SlidingPager()
			});

	var tree = new Ext.ux.tree.TreeGrid({
				region : 'center',
				columns : cm,
				root : root,
				bbar : pagebar
			});
	tree.on('beforeload', function(node, event) {
				if (node.id != 'root') node.loader = new Ext.tree.TreeLoader({
							url : url.getDiscussTree,
							baseParams : {
								did : node.id
							}
						});
			});
	tree.on('click', function(node) {
				showPage(node.id);
			});
	/* endregion */

	/* region newDiscuss */
	var newtitle = new Ext.form.TextField({
				fieldLabel : 'Title',
				allowBlank : false,
				name : 'title',
				anchor : '98%'
			});
	var newcontent = new Ext.form.HtmlEditor({
				fieldLabel : 'Content',
				name : 'content',
				anchor : '98% -30'
			});
	var newform = new Ext.FormPanel({
				frame : true,
				border : false,
				labelAlign : 'right',
				labelWidth : 60,
				items : [newtitle, newcontent],
				fbar : [new Ext.Button({
									text : '重置',
									iconCls : 'reset-icon',
									handler : function() {
										newform.getForm().reset();
									}
								}), new Ext.Button({
									text : '提交',
									iconCls : 'ok-icon',
									disabled : !sys.canDiscuss,
									handler : onpost
								})]
			});
	/* endregion */

	/* region showDiscuss */
	var author = new Ext.form.TextField({
				fieldLabel : 'Author',
				readOnly : true,
				anchor : '100%'
			});
	var time = new Ext.form.TextField({
				fieldLabel : 'Time',
				readOnly : true,
				anchor : '96%'
			});
	var title = new Ext.form.TextField({
				fieldLabel : 'Title',
				readOnly : true,
				anchor : '98%'
			});
	var content = new Ext.ux.HtmlViewer({
				fieldLabel : 'Content',
				anchor : '98% -55'
			});
	var btnreply = new Ext.Button({
				text : '回复',
				iconCls : 'replydiscuss-icon',
				handler : onreply,
				disabled : !sys.canDiscuss
			});
	var btnedit = new Ext.Button({
				text : '编辑',
				iconCls : 'editdiscuss-icon',
				disabled : true,
				handler : onedit
			});
	var toolbarshow = new Ext.Toolbar({
				items : [btnreply, new Ext.Toolbar.Separator(), btnedit]
			});
	if (sys.god) {
		toolbarshow.add(new Ext.Button({
					text : '删除',
					iconCls : 'delete-icon',
					handler : ondel
				}));
	}
	var showform = new Ext.FormPanel({
				frame : true,
				border : false,
				labelAlign : 'right',
				labelWidth : 60,
				items : [{
							layout : 'column',
							items : [{
										columnWidth : .49,
										layout : 'form',
										items : author
									}, {
										columnWidth : .51,
										layout : 'form',
										items : time
									}]
						}, title, content]
			});
	var showpanel = new Ext.Panel({
				layout : 'fit',
				bodyBorder : false,
				items : [showform],
				tbar : toolbarshow
			});
	/* endregion */

	var downpanel = new Ext.Panel({
				layout : 'card',
				activeItem : 0,
				split : true,
				height : 170,
				region : 'south',
				bodyBorder : false,
				items : [newform, showpanel],
				hidden : true,
				collapsible : true,
				collapseMode : 'mini',
				hideCollapseTool : true
			});

	var panel = new Ext.Panel({
				id : 'showDiscussList',
				title : 'DiscussList',
				layout : 'border',
				iconCls : 'discuss-icon',
				closable : true,
				items : [tree, downpanel]
			});
	panel.onRefresh = onRefresh;
	addTab(panel);

	/* region events */
	var did, pid;
	var editdata = null;
	function showNewForm() {
		downpanel.show();
		downpanel.getLayout().setActiveItem(0);
		panel.doLayout();
		downpanel.expand();
	}
	function showShowPanel() {
		downpanel.show();
		downpanel.getLayout().setActiveItem(1);
		panel.doLayout();
		downpanel.expand();
	}
	function hideDownPanel() {
		downpanel.hide();
		panel.doLayout();
	}
	function showPage(vardid) {
		did = vardid;
		editdata = null;
		showform.getForm().reset();
		getDiscuss({
					did : did,
					success : function(data) {
						pid = data.pid;
						author.setValue(sys.rd.authorusername(data));
						title.setValue(data.title);
						time.setValue(data.time);
						content.setValue(data.content);
						btnedit.setDisabled(!(sys.god || sys.user
								&& sys.user.username == data.username));

						showShowPanel();
					}
				});
	}
	function onreply() {
		newtitle.setValue('Re:' + title.getValue());
		newcontent.reset();
		showNewForm();
	}
	function onedit() {
		getDiscuss({
					did : did,
					success : function(data) {
						editdata = data;
						newtitle.setValue(data.title);
						newcontent.setValue(data.content);
						showNewForm();
					}
				});
	}
	function ondel() {
		deleteDiscuss({
					did : did,
					title : title.getValue(),
					success : function() {
						onRefresh(true);
						hideDownPanel();
					}
				});
	}
	function onRefresh(isdel) {
		var node = tree.getSelectionModel().getSelectedNode();
		if (node == null) node = tree.getRootNode();
		if (isdel) node = node.parentNode;
		var path = node.getPath();
		store.reload();
		/*
		 * if (node.isLeaf()) node = node.parentNode; reloadnode();
		 * 
		 * function reloadnode() { if (node.id != 'root') { var thisnode = node;
		 * node = node.parentNode; thisnode.reload(reloadnode); } else
		 * store.reload(function() { tree.selectPath(path); }); }
		 */
	}
	function onpost() {
		if (!newform.getForm().isValid() || !isNoBlank(newcontent)) {
			alertx('请正确输入内容！');
			return;
		}
		var posttype = editdata ? 2 : 1;
		var params = {
			pid : pid
		};
		if (posttype == 2) {
			params.did = did;
			params.parentDid = editdata.parentDid;
		} else
			params.parentDid = did;

		ajaxform({
					form : newform,
					url : url.addOrEditDiscuss,
					params : params,
					success : function(obj) {
						showPage(obj.did);
						onRefresh();
					}
				});
	}
	/* endregion */
}
